<template>
  <div style="margin-top: 20px">
    <el-row>
      <el-col :span="23">
        <el-form ref="form" :model="form" label-width="250px">
          <el-form-item
            :rules="[{ required: true, message: '请输入'}]"
            label="名称"
            prop="Name"
          >
            <el-input v-model="form.Name" />
          </el-form-item>
          <el-form-item label="类型" prop="Type" :rules="[{ required: true, message: '请输入'}]">
            <el-select v-model="form.Type">
              <el-option v-for="item in enums.reportDbTypeEnum" :key="item.value" :label="item.text" :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="Host" prop="Host" :rules="[{ required: true, message: '请输入'}]">
            <el-input v-model="form.Host" />
          </el-form-item>
          <el-form-item label="Port" prop="Port" :rules="[{ required: true, message: '请输入'}]">
            <el-input v-model="form.Port" />
          </el-form-item>
          <el-form-item label="Db name" prop="DbName" :rules="[{ required: true, message: '请输入'}]">
            <el-input v-model="form.DbName" />
          </el-form-item>
          <el-form-item label="Username" prop="Username" :rules="[{ required: true, message: '请输入'}]">
            <el-input v-model="form.Username" />
          </el-form-item>
          <el-form-item label="Password" prop="Password">
            <el-input v-model="form.Password" />
          </el-form-item>
          <el-form-item label="连接池最多空闲链接" prop="MaxIdleConns" :rules="[{ required: true, message: '请输入'},{validator: commonValidator.positiveInt,trigger: 'blur'}]">
            <el-input v-model="form.MaxIdleConns" />
          </el-form-item>
          <el-form-item label="连接池最多链接" prop="MaxOpenConns" :rules="[{ required: true, message: '请输入'},{validator: commonValidator.positiveInt,trigger: 'blur'}]">
            <el-input v-model="form.MaxOpenConns" />
          </el-form-item>
          <el-form-item label="连接可复用的最大时间(单位，秒)" prop="ConnMaxLifetime" :rules="[{ required: true, message: '请输入'},{validator: commonValidator.positiveInt,trigger: 'blur'}]">
            <el-input v-model="form.ConnMaxLifetime" />
          </el-form-item>
          <el-form-item v-if="operateType === enums.operateEnum.ADD">
            <el-button type="primary" @click="add">
              保存
            </el-button>
            <el-button @click="close">
              取消
            </el-button>
          </el-form-item>
          <el-form-item v-if="operateType === enums.operateEnum.DETAIL">
            <el-button @click="close">
              取消
            </el-button>
          </el-form-item>
          <el-form-item v-if="operateType === enums.operateEnum.EDIT">
            <el-button type="primary" @click="edit">
              保存
            </el-button>
            <el-button @click="close">
              取消
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import reportDbApi from '@/api/report_db'
import operateEnum from '@/enums/operateEnum'
import reportDbTypeEnum from '@/enums/reportDb/reportDbTypeEnum'
import commonValidator from '@/utils/commonValidator'

export default {
  name: 'ReportDbInfo',
  props: {
    // id
    id: {
      type: String,
      required: false
    },
    // 操作类型, 可用  edit, add
    operateType: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      // 表单数据
      form: {
        ReportDbId: this.id,
        Name: null,
        Type: null,
        Host: null,
        Port: null,
        DbName: null,
        Username: null,
        Password: null,
        MaxIdleConns: null,
        MaxOpenConns: null,
        ConnMaxLifetime: null
      },
      enums: {
        operateEnum,
        reportDbTypeEnum
      },
      commonValidator
    }
  },
  mounted() {
    if (this.id) {
      this.getDetail()
    }
  },
  methods: {
    getDetail() {
      reportDbApi.detail(this.id).then(response => {
        console.log(response)
        if (response.data.reportDbDetail) {
          var responseData = response.data.reportDbDetail
          responseData.__typename = undefined
          this.form = responseData
        } else {
          this.$notify.error(response.data.msg)
        }
      })
    },
    // 保存数据
    add() {
      // 验证表单数据
      this.$refs.form.validate((valid) => {
        if (valid) {
          reportDbApi.add(this.form).then(response => {
            console.log(response)
            if (response.data.insertReportDb) {
              this.$notify.success('保存成功')
              this.$emit('addComplete')
            } else {
              this.$notify.error(response.errors[0].message)
            }
          }).catch((err) => {
            this.$notify.error(err.message)
          })
        }
      })
    },
    // 保存数据
    edit() {
      // 验证表单数据
      this.$refs.form.validate((valid) => {
        if (valid) {
          reportDbApi.update(this.form).then(response => {
            if (response.data.updateReportDb) {
              this.$notify.success('保存成功')
              this.$emit('editComplete')
            } else {
              this.$notify.error(response.errors[0].message)
            }
          }).catch((err) => {
            this.$notify.error(err.message)
          })
        }
      })
    },
    close() {
      this.$emit('doClose')
    }
  }
}
</script>

<style scoped>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}

.avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>
